﻿
@{
    Layout = null;
    var PicURL = "";
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>云瑞家庭农场</title>
    <link rel="shortcut icon" href="~/Content/assets/i/miemie_favicon.ico" />
    <link href="~/Content/layui/css/layui.css" rel="stylesheet" />
    <script src="~/Content/layui/layui.js"></script>
    <script type="text/javascript" src="/Scripts/jquery-3.3.1.js"></script>
    <script type="text/javascript">
        layui.use(['form', 'layedit', 'laydate'], function () {
            var form = layui.form
                , layer = layui.layer
                , layedit = layui.layedit
                , laydate = layui.laydate;
            
            

            $('#layerDemo .layui-btn').on('click', function () {
                var othis = $(this), method = othis.data('method');
                active[method] ? active[method].call(this, othis) : '';
            });
            
            
            $(document).ready(function () {
                var _token = cookie("token");
                var OrderId = getQueryString("Id");
                $.ajax({//右上角用户登录信息
                    url: "/YunRuiFarmAPI/UserInfoGetMessageAPI", //请求的URL
                    cache: true, //不从缓存中取数据
                    data: {
                        "Token": _token,
                    }, //发送的参数
                    type: 'post', //请求类型
                    dataType: 'json', //返回类型是JSON
                    timeout: 20000, //超时
                })
                    .done(function (result) {
                        $("#Name").html("<img src= \"/Content/image/小埋1.jpg\" class=\"layui-nav-img\"/>" + result.Name)
                    });
                $.ajax({
                    url: "/YunRuiFarmAPI/AdministratorsPictureGetPlaceAPI", //请求的URL
                    cache: true, //不从缓存中取数据
                    data: {
                    }, //发送的参数
                    type: 'get', //请求类型
                    dataType: 'json', //返回类型是JSON
                    timeout: 20000, //超时
                    error: function (xhr, err) {
                        layer.msg("编辑失败");
                    },
                    success: function (result) {
                        var str = "<option value=\"0\" selected=\"\">未选择</option>";
                        for (var i = 0; i < result.length; i++) {
                            str += "<option value=\"" + result[i].Id + "\">" + result[i].Place + "</option>";
                        }
                        $("#Place").html(str);  
                        if (getQueryString("Id") != 0) {
                            $.ajax({
                                url: "/YunRuiFarmAPI/AdministratorsPictureMessageAPI",
                                cache: true, //不从缓存中取数据
                                data: {
                                    "Id": getQueryString("Id"),
                                }, //发送的参数
                                type: 'post', //请求类型
                                dataType: 'json', //返回类型是JSON
                                timeout: 20000, //超时
                                error: function (xhr, err) {
                                    layer.msg("请输入正确的Id");
                                },
                                success: function (data) {
                                    //PicURL = result.PicUrl;
                                    for (var i = 0; i < result.length; i++) {
                                        if (data.Place == result[i].Place) {
                                            $("#Place").val(result[i].Id);
                                            break;
                                        }
                                    }
                                    $("#PicUrl").val(data.PicUrl);
                                    $("#demo1")[0].style.backgroundImage = 'url(' + data.PicUrl+')'
                                    $.ajax({
                                        url: "/YunRuiFarmAPI/AdministratorsPictureGetDetailAPI", //请求的URL
                                        cache: true, //不从缓存中取数据
                                        data: {
                                            "Id": data.Id
                                        }, //发送的参数
                                        type: 'get', //请求类型
                                        dataType: 'json', //返回类型是JSON
                                        timeout: 20000, //超时
                                        error: function (xhr, err) {
                                            layer.msg("编辑失败");
                                        },
                                        success: function (message) {
                                            var str = "<option value=\"0\" selected=\"\">未选择</option>";
                                            for (var i = 0; i < message.length; i++) {
                                                str += "<option value=\"" + message[i].Id + "\">" + message[i].Place + "</option>";
                                            }
                                            $("#Detail").html(str);
                                            for (var i = 0; i < message.length; i++) {
                                                if (message[i].Place == data.Detail) {
                                                    $("#Detail").val(message[i].Id);
                                                    break;
                                                }
                                            }

                                        }
                                    })
                                }
                            })
                        }
                    }
                })
                $("#Save").click(function () {
                    $.ajax({
                        url: "/YunRuiFarmAPI/AdministratorsPictureSaveAPI", //请求的URL
                        cache: true, //不从缓存中取数据
                        data: {
                            "Id": getQueryString("Id"),
                            "Place": $("#Place option:selected").text(),
                            "Detail": $("#Detail option:selected").text(),
                            "PicUrl": $("#PicUrl").val()
                        }, //发送的参数
                        type: 'get', //请求类型
                        dataType: 'json', //返回类型是JSON
                        timeout: 20000, //超时
                        error: function (xhr, err) {
                            layer.msg(xhr.msg);
                        },
                        success: function (message) {
                            layer.msg("保存成功");
                            setTimeout(function () {
                                window.location.href = '/YunRuiFarm/AdministratorsPictureList'
                            }, 3000);
                        }
                    })
                });
                $("#Cancellation").click(function () {
                    layer.msg("清除token");
                    delcookie("token", "", { expires: -1 });
                    window.location.href = "/YunRuiFarm/Login";
                });
                $("#Return").click(function () {
                    window.location.href = "/YunRuiFarm/AdministratorsPictureList";
                });
            });
            
            function getQueryString(name) {
                var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
                var r = window.location.search.substr(1).match(reg);
                if (r != null) {
                    return unescape(r[2]);
                }
                return 0;
            }
            function cookie(key, value, options) {
                if (typeof value === "undefined") {
                    var cookies = document.cookie.split("; ");
                    for (var i = 0, len = cookies.length; i < len; i++) {
                        var cookie = cookies[i].split("=");
                        if (decodeURIComponent(cookie[0]) === key) {
                            return decodeURIComponent(cookie[1]);
                        }
                    }
                    return null;
                }
            }
            function delcookie(key, value, options) {
                options = options || {};
                var cookie = encodeURIComponent(key) + "=" + value;
                if ((typeof options.expires) !== "undefined") {
                    if (typeof options.expires === "number") {
                        var days = options.expires,
                            t = options.expires = new Date();
                        t.setDate(t.getDate() + days);
                    }
                    cookie += ";expires=" + options.expires.toUTCString();
                }
                if (typeof options.path !== "undefined")
                    cookie += ";path=" + options.path;
                if (typeof options.domain !== "undefined")
                    cookie += ";domain=" + options.domain;
                if (options.secure)
                    cookie += ";secure";
                document.cookie = cookie;
            }
            $("#power").click(function () {
                $.ajax({
                    url: "/YunRuiFarmAPI/GetAdministratorsPower", //请求的URL
                    cache: true, //不从缓存中取数据
                    data: {
                        "Token": _token
                    }, //发送的参数
                    type: 'post', //请求类型
                    dataType: 'json', //返回类型是JSON
                    timeout: 20000, //超时
                    error: function (xhr, err) {

                    },
                    success: function (result) {
                        if (result.IsSuccess)
                            window.location.href = result.Message;
                    }
                })
            });
        });
        function selectdetail() {
            if ($("#Place option:selected").val() != 0)
                $.ajax({
                    url: "/YunRuiFarmAPI/AdministratorsPictureGetDetailAPI2", //请求的URL
                    cache: true, //不从缓存中取数据
                    data: {
                        "Id": $("#Place option:selected").val(),
                    }, //发送的参数
                    type: 'get', //请求类型
                    dataType: 'json', //返回类型是JSON
                    timeout: 20000, //超时
                    error: function (xhr, err) {
                        layer.msg("编辑失败");
                    },
                    success: function (result) {
                        var str = "<option value=\"0\" selected=\"\">未选择</option>";
                        for (var i = 0; i < result.length; i++) {
                            str += "<option value=\"" + result[i].Id + "\">" + result[i].Place + "</option>";
                        }
                        $("#Detail").html(str);
                    }
                })
        }
    </script>
    <script>
        layui.use('upload', function () {
            var $ = layui.jquery
                , upload = layui.upload;

            //普通图片上传
            var uploadInst = upload.render({
                elem: '#test1'
                , url: '/YunRuiFarmAPI/UploadFile'
                , before: function (obj) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                        $('#demo1').attr('src', result); //图片链接（base64）
                        layer.msg("上传成功");
                        $("#PicUrl").val("/Content/image/"+file.name);
                    });
                    
                }
                , done: function (res) {
                    //如果上传失败
                    if (res.code > 0) {
                        return layer.msg('上传失败');
                    }
                    //上传成功
                    esle
                    return layer.msg('上传成功');
                }
                , error: function () {
                    //演示失败状态，并实现重传
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function () {
                        uploadInst.upload();
                    });
                }
            });
        });
    </script>
    <style>
        body {
            font-size: 18px;
        }

        #divtitle {
            margin: 0 auto;
            margin-top: 0px;
            margin-bottom: 0px;
        }


        #content-right {
            width: 100%;
            height: 100%;
            float: right
        }

        #content-right-top {
            margin-top: 2%;
            margin-right: 2%;
            margin-left: 2%;
            height: 20%;
            width: 96%;
            float: left
        }

        #content-right-button {
            margin-right: 2%;
            margin-left: 2%;
            margin-top: 1%;
            height: 88%;
            width: 96%;
            float: left;
            background-color: white;
        }

        #content-right-button-table {
            margin-left: 10%;
            margin-top: 1%;
            height: 64%;
            width: 96%;
            float: left;
        }


        img {
            width: 100%;
            height: 100%;
        }
    </style>
</head>
<body class="layui-layout-body">
    <div class="layui-layout layui-layout-admin">
        <div class="layui-header" style="width:auto">
            <div class="layui-logo" style=" background-image: url('../../Content/image/logo.jpg');background-size:100% 100%;"></div>
            <!-- 头部区域（可配合layui已有的水平导航） -->
            <ul class="layui-nav layui-layout-left" style="font-size: 20px;">
                <li class="layui-nav-item"><a href="/YunRuiFarm/Index">首页</a></li>
                <li class="layui-nav-item"><a href="/YunRuiFarm/Adopt">羊领养预约</a></li>
                <li class="layui-nav-item"><a href="/YunRuiFarm/Agricultural">农副产品预约</a></li>
                <li class="layui-nav-item"><a href="/YunRuiFarm/Restaurant">餐饮预约</a></li>
            </ul>
            <ul class="layui-nav layui-layout-right">
                <li class="layui-nav-item">
                    <a href="javascript:;" id="Name">
                        <img src="/Content/image/小埋1.jpg" class="layui-nav-img" />贤心
                    </a>
                    <dl class="layui-nav-child">
                        <dd><a href="/YunRuiFarm/UserInfo">基本资料</a></dd>
                        <dd><a href="/YunRuiFarm/UserRestaurantOrder">我的订单</a></dd>
                        <dd><a href="/YunRuiFarm/UserFeedback">反馈信息</a></dd>
                    </dl>
                </li>
                <li class="layui-nav-item">
                    <a href="javascript:;">账户操作</a>
                    <dl class="layui-nav-child">
                        <dd><a href="javascript:;" id="power">切换到管理员</a></dd>
                        <dd><a href="javascript:;" id="Cancellation">注销</a></dd>
                    </dl>
                </li>
            </ul>
        </div>
        <div class="layui-side layui-bg-black">
            <div class="layui-side-scroll">
                <!-- 左侧导航区域（可配合layui已有的垂直导航） -->
                <ul class="layui-nav layui-nav-tree" lay-filter="test">
                    <li class="layui-nav-item layui-nav-itemed">
                        <a class="" href="javascript:;">用户管理</a>
                        <dl class="layui-nav-child">
                            <dd><a href="/YunRuiFarm/AdministratorsGoatOrder">羊领养订单</a></dd>
                            <dd><a href="/YunRuiFarm/AdministratorsRestaurantOrder">餐饮订单</a></dd>
                            <dd><a href="/YunRuiFarm/AdministratorsAgriculturalOrder">农副产品订单</a></dd>
                            <dd><a href="/YunRuiFarm/AdministratorsFeedback">用户反馈</a></dd>
                        </dl>
                    </li>
                </ul>
                <ul class="layui-nav layui-nav-tree" lay-filter="test">
                    <li class="layui-nav-item layui-nav-itemed">
                        <a class="" href="javascript:;">网页管理</a>
                        <dl class="layui-nav-child">
                            <dd><a href="/YunRuiFarm/AdministratorsPictureList">图片管理</a></dd>
                            <dd><a href="/YunRuiFarm/AdministratorsAgriculturalList">农副产品管理</a></dd>
                        </dl>
                    </li>
                </ul>
            </div>
        </div>
        <div class="layui-body" style="background-color:#ECF0F5;height:100%;">
            <!-- 内容主体区域 -->
            <div id="content-right">
                <div id="content-right-button">
                    <div class="layui-form-item" style=" margin-top:30px">
                        <label class="layui-form-label">图片位置</label>
                        <div class="layui-input-inline">
                            <select onchange="selectdetail()" id="Place" style="width: 161px;height: 38px;">
                                <option value="0" selected="">未选择</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">详细信息</label>
                        <div class="layui-input-inline">
                            <select id="Detail" style="width: 161px;height: 38px;">
                                <option value="0" selected="">未选择</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">图片地址</label>
                        <div class="layui-input-inline" style=" width:40%">
                            <input type="text" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input" id="PicUrl">
                        </div>
                    </div>
                    <div class="layui-upload" style="margin-left:5%">
                        <button type="button" class="layui-btn" id="test1">上传图片</button>
                        <p>仅支持jpg和png格式</p>
                        <div class="layui-upload-list">
                            <img class="layui-upload-img" id="demo1" style="height:250px;width:400px;background-size:100% 100%;" >
                            <p id="demoText"></p>
                        </div>
                    </div>
                    <div id="content-button">
                        <button class="layui-btn" lay-submit="" type="button" id="Save" style="float:left;margin-left:5%;">保 存</button>
                        <button class="layui-btn" lay-submit="" type="button" id="Return" style="float:left;">返 回</button>
                    </div>
                </div>
            </div>
        </div>


    </div>
    <script>

    </script>
</body>
</html>
